Rem this is a utility routine, it deletes all the temp files this
Rem program has left laying around.
Rem
Rem notes: (1) it assumes files are in the root of drive P:, my ramdisk
Rem (2) it is dangerous - can easily delete too much
Rem
While Exist("p:f*")
Kill "p:f*"
Wend
Return
Procedure Sort_references
Rem
Rem this routine does some overhead for a merge sort of the disk file
Rem containing the list of references
Rem
Print "sorting - pass ";
Lnox%=Crscol
Lnoy%=Crslin
Pass%=0
Qqqinputbuf$="" ! to free memory
Void Fre(0)
Open "O",#1,Tempfile2$ ! create null file
Close #1 ! done with it
Repeat
@Sort_one_pass
Until Not Side2!
Kill Tempfile2$
Kill Tempfile3$
Kill Tempfile4$
Return
Procedure Sort_one_pass
Rem
Rem this routine does one pass of a merge sort
Rem at present it's strictly a two-by merge.
Rem
Inc Pass% ! print pass# so user doesn't get too impatient
Print At(Lnox%,Lnoy%);Pass%
Rcnt%=0 ! initialize record and segment counters
Seg%=1
Open "I",#1,Tempfile1$ ! open files
Open "I",#2,Tempfile2$
Open "O",#3,Tempfile3$
Open "O",#4,Tempfile4$
Side2!=False ! haven't used the second output file yet
Ofile!=False ! switch for which output file to use
F1oc!=False ! input record buffers not occupied
F2oc!=False
Old$="" ! no previous record
Oldr=0
Repeat
If Not F1oc! ! should fill buffer 1, preferably from input 1
On Error Gosub Errone
If Not Eof(#1)
Input #1,Ref1,Var1$ ! read the record
Inc Rcnt% ! count it
F1oc!=True ! buffer now occupied
Else ! have to read from input 2 instead
If Not Eof(#2) ! if we can
Input #2,Ref1,Var1$ ! read the record
Inc Rcnt% ! count it
F1oc!=True ! buffer occupied
Endif
Endif
Endif
Eofone:
If Not F2oc! ! should fill buffer 2 - see above
On Error Gosub Errtwo
If Not Eof(#2)
Input #2,Ref2,Var2$
Inc Rcnt%
F2oc!=True
Else
If Not Eof(#1)
Input #1,Ref2,Var2$
Inc Rcnt%
F2oc!=True
Endif
Endif
Endif
Eoftwo:
On Error ! normal error handling
If ((Var1$<Old$ Or (Var1$=Old$ And Ref1<Oldr)) Or Not F1oc!) And ((Var2$<Old$ Or (Var2$=Old$ And Ref2<Oldr)) Or Not F2oc!)
Rem have completed a "run", must switch output files
Ofile!=Not Ofile! ! switch the indicator
Inc Seg% ! count the next run
Oldr=0 ! no previous record in the run
Old$=""
Endif
If F1oc! And (Var1$>Old$ Or (Var1$=Old$ And Ref1>=Oldr)) And ((Var1$<Var2$ Or (Var1$=Var2$ And Ref1<=Ref2)) Or (Not F2oc!) Or (Var2$<Old$ Or (Var2$=Old$ And Ref2<Oldr)))
Rem should write record in buffer 1
@Sort_out(Ref1,Var1$) ! call routine to write it
F1oc!=False ! now the buffer's empty
Else
If F2oc! And (Var2$>Old$ Or (Var2$=Old$ And Ref2>=Oldr)) And ((Var2$<Var1$ Or (Var2$=Var1$ And Ref2<=Ref1)) Or (Not F1oc!) Or (Var1$<Old$ Or (Var1$=Old$ And Ref1<Oldr)))
Rem should write record in buffer 2
@Sort_out(Ref2,Var2$) ! call routine to write it
F2oc!=False ! now the buffer's empty
Rem if both buffers are occupied or both files are at eof, should always
Rem be writing one of the two buffers out.
Endif
Endif
Until Eof(#1) And Eof(#2) And Not (F1oc! Or F2oc!)